﻿using System;
using System.Collections;
using COC.Algorithm;
using COC.Character;
using COC.Tool;
using COC.Tools.Pathing.Test;
using UnityEngine;
using Random = UnityEngine.Random;

namespace COC.Tools.Pathing
{
    public class MovePath : BaseBehaviourSupport
    {

        [HideInInspector]
        public AIPathFinding findingPath;
        [HideInInspector]
        public int id = 0;
        public float mTileSize;
        public float mOffset;
        public float startingX;
        public float startingZ;
        public float speed = .06f;
        private int step = 0;
        Transform clone;
        public Transform targetTransform;
        #region A*
        [NonSerialized]
        Vector3 target;
        Vector3 cacheTargetPos;
        Vector3 cacheOriginPos;
        CPath path;
        private Vector3 origin;
        #endregion
        bool searchAgain = true; //seart to next search path if previous has finished. 
        bool cruising = false;

       
        public override void OnStart()
        {
            // origin = transform.localPosition;
            // target = transform.localPosition;
            path=new CPath();
            path.id = GenerateUniqueNumber.getInstance().getNewNumber();
            findingPath=GameObject.FindObjectOfType<AIPathFinding>();
            StartCoroutine(DoSearchPath());
            StartCoroutine(UpdatePosition(findingPath));


        }
        Vector3 currMousePos = Vector3.zero;
        void Update()
        {
            if(false)
            if (Input.GetMouseButtonUp(0))
            {
                currMousePos = Vector3.zero;
                currMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                currMousePos = new Vector3(currMousePos.x, EnchanceMap.characterHeightPositon, currMousePos.z);
                targetTransform.localPosition = currMousePos;
                target = currMousePos;
                if (findingPath != null && path.STATE==PathInfo.PathState.ONE&&path.mPathInfo.length>0)
                {
                    path.STATE = PathInfo.PathState.NONE;
                    path.mPathInfo.length = 0;
                    path.mPathInfo.location = 0;
                }
                
                StartCoroutine(DoSearchPath());

            }
        }

      
        IEnumerator UpdatePosition(AIPathFinding findingPath)
        {
            while (true)
            {
               
                if (findingPath != null && (path.STATE == PathInfo.PathState.ONE) && path.mPathInfo.length > 0)
                {
                    if (path.mPathInfo.location == path.mPathInfo.length)
                    {
                        path.STATE = PathInfo.PathState.NONE;
                        path.mPathInfo.length = 0;
                        path.mPathInfo.location = 0;
                        cruising = true;
                        StartCoroutine(DoSearchPath());
                    }
                    Vector3 targetPos = findingPath.ReadPath2(id, origin.x, origin.z,origin.y);
//                    float distTotal = (targetPos - origin).magnitude;
                    if (targetPos != Vector3.zero && origin != targetPos)
                        this.gameObject.transform.localPosition = Vector3.Lerp(origin, targetPos, speed * Time.smoothDeltaTime);
                    origin = targetPos;

                }
                yield return new WaitForSeconds(.2f);
            }
        }

        public override void OnDestroy()
        {
            //StopCoroutine("UpdatePosition");
            StopAllCoroutines();
        }


        public void schedulePath(Vector3 starting, Vector3 destination)
        {
            if (starting != destination)
            {
                origin = starting;
                target = destination;
            }
        }
        IEnumerator DoSearchPath()
        {

            if (findingPath != null && origin != target)
            {

                UnityEngine.Debug.Log("ZZZZZZZZZZZZZZZZZZZZ");
                path.startingXY = origin;
                path.targetXY = target;
                yield return new WaitForSeconds(1.0f/Random.Range(1.0f,9.0f));
                findingPath.pushPath(path);

            }
            if (cruising)
            {

                UnityEngine.Debug.Log("ZZZZZZZZZZZZZZZZZZZZ");
                path.startingXY = target;
                path.targetXY = origin;
                yield return new WaitForSeconds(1.0f / Random.Range(1.0f, 9.0f));
                findingPath.pushPath(path);
                cruising = false;

            }
                yield return null;

        }
    }

}
